﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Bll;
using AspNetCs.Model;
using AspNetCs.Common;
using System.Data;

public partial class Master_Admin_City : Admin_BasePage
{
    Province p = new Province();
    protected void Page_Load(object sender, EventArgs e)
    {
        IsExit();
        if (!Page.IsPostBack)
        {
            bind();

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Province_Model model = new Province_Model();
        model.ClassName = StringHelper.HtmlEncrypt(this.Txt_Prov.Text.Trim());
        int Result = p.Add(model);
        if (Result == 0)
        {
            Response.Write("<script>alert('添加成功!');location.href='Admin_City.Aspx';</script>");
            Response.End();
        }
        else
        {
            Response.Write("<script>alert('省份添加失败!');history.back();</script>");
            Response.End();
        }
    }
    protected void bind()
    {
        DataTable dt = p.ProvinceList();
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
            string NewValue = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("NewClassName")).Text;
            if (NewValue == "")
            {
                Response.Write("<script>alert('分类名称不能为空')</script>");
                GridView1.EditIndex = e.RowIndex;
            }
            else
            {
                Province_Model model = new Province_Model();
                model.ClassName = NewValue;
                model.Id = Convert.ToInt32(id);
                p.update(model);
                GridView1.EditIndex = -1;
            }
            bind();
        }
        finally { }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        p.del(Convert.ToInt32(id));
        GridView1.EditIndex = -1;
        bind();

    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Orders")
        {
            p.UpdateOrder(StringHelper.isNumber_int(e.CommandArgument.ToString()));
            Response.Redirect(configInfo.InstallDir + configInfo.AdminDir + "/Admin_City.Aspx");
            Response.End();
        
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#ffcc00'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
        }


    }
}
